<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>添加度量指标 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_adding_a_metric_to_the_mix.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_adding_a_metric_to_the_mix.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_adding_a_metric_to_the_mix.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_adding_a_metric_to_the_mix.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_aggregation_test_drive.html">尝试聚合</a></span>
»
<span class="breadcrumb-node">添加度量指标</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_aggregation_test_drive.html">« 尝试聚合</a>
</span>
<span class="next">
<a href="_buckets_inside_buckets.html">嵌套桶 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_adding_a_metric_to_the_mix"></a>添加度量指标(Metric)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/21_add_metric.asciidoc">edit</a>
</h2>
</div></div></div>
<p>前面的例子告诉我们每个桶里面的文档数量，这很有用。但通常，我们的应用需要提供更复杂的关于文档的度量。
例如，每种颜色(color)汽车的平均价格是多少？</p>
<p>为了获取更多信息，我们需要告诉 Elasticsearch 使用哪个字段(field)，计算何种度量。 这需要将度量 <em>嵌套(nesting)</em> 在桶内，
度量会基于桶内的文档计算统计结果。</p>
<p>让我们继续为汽车的例子加入  <code class="literal">average</code> 平均度量：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "colors": {
         "terms": {
            "field": "color"
         },
         "aggs": { <a id="CO186-1"></a><i class="conum" data-value="1"></i>
            "avg_price": { <a id="CO186-2"></a><i class="conum" data-value="2"></i>
               "avg": {
                  "field": "price" <a id="CO186-3"></a><i class="conum" data-value="3"></i>
               }
            }
         }
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/20_basic_example.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO186-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>为度量新增 <code class="literal">aggs</code> 层: 嵌套了一层<code class="word">price</code>的统计。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO186-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>为嵌套的度量指定名字： <code class="word">avg_price</code> 。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO186-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>最后，为 <code class="word">price</code> 字段定义 <code class="literal">avg</code> 度量。</p>
</td>
</tr>
</table>
</div>
<p>正如所见，我们用前面的例子加入了新的 <code class="literal">aggs</code> 层。这个新的聚合层让我们可以将 <code class="literal">avg</code> 度量嵌套置于 <code class="literal">terms</code> 桶内。实际上，这就为每个颜色生成了平均价格。</p>
<p>正如 <code class="word">颜色</code> 的例子，我们需要给度量起一个名字（ <code class="word">avg_price</code> ）这样可以稍后根据名字获取它的值。最后，我们指定度量本身（ <code class="literal">avg</code> ）以及我们想要计算平均值的字段（ <code class="word">price</code> ）：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
...
   "aggregations": {
      "colors": {
         "buckets": [
            {
               "key": "red",
               "doc_count": 4,
               "avg_price": { <a id="CO187-1"></a><i class="conum" data-value="1"></i>
                  "value": 32500
               }
            },
            {
               "key": "blue",
               "doc_count": 2,
               "avg_price": {
                  "value": 20000
               }
            },
            {
               "key": "green",
               "doc_count": 2,
               "avg_price": {
                  "value": 21000
               }
            }
         ]
      }
   }
...
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO187-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>响应中的新字段 <code class="word">avg_price</code> 。</p>
</td>
</tr>
</table>
</div>
<p>尽管响应只发生很小改变，实际上我们获得的数据更多了。之前，我们知道有四辆红色的车，现在，红色车的平均价格是 $32，500 美元。这个信息可以直接显示在报表或者图形中。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_aggregation_test_drive.html">« 尝试聚合</a>
</span>
<span class="next">
<a href="_buckets_inside_buckets.html">嵌套桶 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>